package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("FoodController")
public class FoodController extends CmiEmaController {

	@RequestMapping("/listFoods")
	public ModelAndView listFoods(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = foodPaginationService.updatePagination(request, pagination);
		Set<Food> foods = foodPaginationService.getPaginationFoods(pagination);

		mav.addObject("foods", foods);
		mav.addObject("entityName", "food");
		mav.addObject("pagination", pagination);
		mav.setViewName("food/listFoods");
		return mav;
	}

	public String listFoods() {
		return "redirect:/listFoods";
	}

	@RequestMapping("/viewFood")
	public ModelAndView viewFood(@RequestParam Integer foodId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("food", foodDAO.findFoodByPrimaryKey(foodId));
		mav.setViewName("food/viewFood");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFoods")
	public ModelAndView confirmDeleteFoods(
			@RequestParam(value = "deleteFoodsIds", required = true) Set<Integer> deleteFoodsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Food> deleteFoods = foodDAO.findFoodsByIds(deleteFoodsIds);
		Set<Food> referencedFoods = foodService.loadReferenceFoods(deleteFoods);
		boolean hasReferenced;
		if (referencedFoods.size() == 0) {
			mav.addObject("deleteEntitys", deleteFoods);
			hasReferenced = false;
		} else {
			deleteFoods.removeAll(referencedFoods);
			mav.addObject("referencedEntitys", referencedFoods);
			mav.addObject("deleteEntitys", deleteFoods);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Food");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFoods");
		return mav;
	}

	@RequestMapping("/deleteFood")
	public String deleteFood(@RequestParam Integer foodId) {
		Food food = foodDAO.findFoodByPrimaryKey(foodId);
		foodService.deleteFood(food);
		return "forward:/listFoods";
	}

	@RequestMapping("/confirmDeleteFood")
	public ModelAndView confirmDeleteFood(@RequestParam Integer foodId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("food", foodDAO.findFoodByPrimaryKey(foodId));

		mav.setViewName("food/deleteFood");

		return mav;
	}

	@RequestMapping("/deleteFoods")
	public String deleteFoods(@RequestParam(value = "deleteFoodsId", required = true) Set<Integer> deleteFoodsId) {
		foodService.deleteFoods(deleteFoodsId);
		return "forward:/listFoods";
	}

	@RequestMapping("/editFood")
	public ModelAndView editFood(@RequestParam Integer foodId) {

		ModelAndView mav = new ModelAndView();

		Food food = foodDAO.findFoodByPrimaryKey(foodId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("food", food);

		// 把所有的引用food对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodService.loadFoodObjectReferencedSet(food));

		// 将编辑页面food添加到mav中
		mav.setViewName("food/editFood");
		return mav;
	}

	@RequestMapping("/newFood")
	public ModelAndView newFood() {
		ModelAndView mav = new ModelAndView();
		Food food = new Food();

		mav.addObject("food", food);

		// 把所有的引用food对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodService.loadFoodObjectReferencedSet(food));

		mav.addObject("newFlag", true);
		mav.setViewName("food/editFood");

		return mav;
	}

	@RequestMapping("/saveFood")
	public ModelAndView saveFood(@ModelAttribute Food food) {

		ModelAndView mav = new ModelAndView();

		Brand brandTemp = food.getBrand();
		Brand brand = brandTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(brandDAO.findBrandById(brandTemp.getId()));
		Expire expireTemp = food.getExpire();
		Expire expire = expireTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(expireDAO.findExpireById(expireTemp.getId()));
		Storemethod storemethodTemp = food.getStoremethod();
		Storemethod storemethod = storemethodTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(storemethodDAO.findStoremethodById(storemethodTemp.getId()));
		Producer producerTemp = food.getProducer();
		Producer producer = producerTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(producerDAO.findProducerById(producerTemp.getId()));
		Foodtype foodtypeTemp = food.getFoodtype();
		Foodtype foodtype = foodtypeTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(foodtypeDAO.findFoodtypeById(foodtypeTemp.getId()));
		Productcode productcodeTemp = food.getProductcode();
		Productcode productcode = productcodeTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(productcodeDAO.findProductcodeById(productcodeTemp.getId()));
		Taste tasteTemp = food.getTaste();
		Taste taste = tasteTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(tasteDAO.findTasteById(tasteTemp.getId()));

		Food existingFood = CmiHibernateProxyUtil.deproxy(foodDAO.findFoodByPhysicalPrimaryKey(food.getName(),
				foodtype.getId(), productcode.getId(), brand.getId()));

		food.setBrand(brand);
		food.setExpire(expire);
		food.setStoremethod(storemethod);
		food.setProducer(producer);
		food.setFoodtype(foodtype);
		food.setProductcode(productcode);
		food.setTaste(taste);
		// 新建记录
		if (food.getId() == null) {
			if (food.equals(existingFood)) {

				mav.addAllObjects(foodService.loadFoodClassReferencedSet(food));
				// mav = editContinue(mav, food);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("food/editFood");
			} else {
				foodService.saveFood(food);
				mav.setViewName("forward:/listFoods");
			}
		}
		// 编辑记录
		else {
			Food originFood = CmiHibernateProxyUtil.deproxy(foodDAO.findFoodByPrimaryKey(food.getId()));
			if (!food.equals(originFood) && food.equals(existingFood)) {
				mav.addAllObjects(foodService.loadFoodClassReferencedSet(food));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("food/editFood");
			} else {
				foodService.saveFood(food);
				mav.setViewName("forward:/listFoods");
			}
		}
		return mav;
	}

	@RequestMapping("/foodController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
